# == Schema Information
# Schema version: 6
#
# Table name: entries
#
#  id          :integer(11)     not null, primary key
#  category_id :integer(11)     
#  title       :string(255)     default(""), not null
#  content     :text            default(""), not null
#  count       :integer(11)     default(0)
#  created_at  :datetime        
#  updated_at  :datetime        
#

class Entry < ActiveRecord::Base
  belongs_to :category, :counter_cache => true
  acts_as_taggable
  acts_as_commentable
  
  validates_presence_of :title, :content 
  validates_length_of :title, :maximum => 40
  
  def self.latest_entries_on(user, count)
    sql = 'SELECT DISTINCT e.* ' +
    'FROM entries e, categories c ' +
    'WHERE c.blog_id = ? AND e.category_id = c.id ' +
    'ORDER BY e.created_at DESC LIMIT ?'
    find_by_sql([sql, user.blog.id, count])
  end
  
end
  